package cn.stylefeng.guns.modular.basic.controller;

import cn.stylefeng.guns.base.pojo.page.LayuiPageInfo;
import cn.stylefeng.guns.modular.basic.entity.BasicCarrier;
import cn.stylefeng.guns.modular.basic.entity.BasicCarrierLine;
import cn.stylefeng.guns.modular.basic.entity.BasicLine;
import cn.stylefeng.guns.modular.basic.entity.BasicOperate;
import cn.stylefeng.guns.modular.basic.model.params.BasicCarrierLineParam;
import cn.stylefeng.guns.modular.basic.model.params.BasicCarrierParam;
import cn.stylefeng.guns.modular.basic.model.params.BasicLineParam;
import cn.stylefeng.guns.modular.basic.model.result.BasicCarrierResult;
import cn.stylefeng.guns.modular.basic.model.result.BasicLineResult;
import cn.stylefeng.guns.modular.basic.service.BasicCarrierLineService;
import cn.stylefeng.guns.modular.basic.service.BasicCarrierService;
import cn.stylefeng.guns.modular.basic.service.BasicLineService;
import cn.stylefeng.guns.modular.basic.service.BasicOperateService;
import cn.stylefeng.guns.sys.modular.system.entity.User;
import cn.stylefeng.guns.sys.modular.system.service.UserService;
import cn.stylefeng.roses.core.base.controller.BaseController;
import cn.stylefeng.roses.core.reqres.response.ResponseData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;
import java.util.List;


/**
 * 承运商线路关联表控制器
 *
 * @author like
 * @Date 2024-10-10 16:36:29
 */
@Controller
@RequestMapping("/basicCarrierLine")
public class BasicCarrierLineController extends BaseController {

    private String PREFIX = "/basic/basicCarrierLine";

    @Autowired
    private BasicCarrierLineService basicCarrierLineService;

    @Autowired
    private UserService userService;

    @Autowired
    private BasicCarrierService basicCarrierService;

    @Autowired
    private BasicLineService basicLineService;

    @Autowired
    private BasicOperateService basicOperateService;

    /**
     * 跳转到主页面
     *
     * @author like
     * @Date 2024-10-10
     */
    @RequestMapping("")
    public String index(Model model) {
        User user = userService.getcurrentUser();
        Long deptId = user.getDeptId();
        Long deliverId = user.getDeliverId();

        //查询可选择分公司
        model.addAttribute("basicOperatelist", basicOperateService.findAvailableList(deptId,deliverId,user.getOperateId()));
        return PREFIX + "/basicCarrierLine.html";
    }

    /**
     * 新增页面
     *
     * @author like
     * @Date 2024-10-10
     */
    @RequestMapping("/add")
    public String add(Model model) {
        // 承运商名称 线路名称 项目
        User user = userService.getcurrentUser();
        Long deptId = user.getDeptId();
        Long deliverId = user.getDeliverId();
        //承运商
        BasicCarrierParam carrierParam = new BasicCarrierParam();
        carrierParam.setDeliverId(deliverId);
        carrierParam.setDeptId(deptId);
        carrierParam.setOperateId(user.getOperateId());
        List<BasicCarrierResult> carrierLists = basicCarrierService.findListBySpec(carrierParam, user.getOperateId());
        model.addAttribute("carrierLists", carrierLists);
        //线路
        BasicLineParam lineParam = new BasicLineParam();
        lineParam.setDeliverId(deliverId);
        lineParam.setDeptId(deptId);
        if(user.getOperateId() != null){
            lineParam.setOperateId(user.getOperateId().intValue());
        }
        List<BasicLineResult> lineLists = basicLineService.findListBySpec(lineParam, user.getOperateId());
        model.addAttribute("lineLists", lineLists);

        return PREFIX + "/basicCarrierLine_add.html";
    }

    /**
     * 编辑页面
     *
     * @author like
     * @Date 2024-10-10
     */
    @RequestMapping("/edit")
    public String edit(Model model) {
        User user = userService.getcurrentUser();
        Long deptId = user.getDeptId();
        Long deliverId = user.getDeliverId();
        //承运商
        BasicCarrierParam carrierParam = new BasicCarrierParam();
        carrierParam.setDeliverId(deliverId);
        carrierParam.setDeptId(deptId);
        carrierParam.setOperateId(user.getOperateId());
        List<BasicCarrierResult> carrierLists = basicCarrierService.findListBySpec(carrierParam, user.getOperateId());
        model.addAttribute("carrierLists", carrierLists);
        //线路
        BasicLineParam lineParam = new BasicLineParam();
        lineParam.setDeliverId(deliverId);
        lineParam.setDeptId(deptId);
        if(user.getOperateId() != null){
            lineParam.setOperateId(user.getOperateId().intValue());
        }
        List<BasicLineResult> lineLists = basicLineService.findListBySpec(lineParam, user.getOperateId());
        model.addAttribute("lineLists", lineLists);
        return PREFIX + "/basicCarrierLine_edit.html";
    }

    /**
     * 新增接口
     *
     * @author like
     * @Date 2024-10-10
     */
    @RequestMapping("/addItem")
    @ResponseBody
    public ResponseData addItem(BasicCarrierLineParam basicCarrierLineParam) {
        User user = userService.getcurrentUser();
        Integer carrierId = basicCarrierLineParam.getCarrierId();
        BasicCarrier byId1 = basicCarrierService.getById(carrierId);
        Integer lineId = basicCarrierLineParam.getLineId();
        BasicLine byId2 = basicLineService.getById(lineId);
        Long operateId = user.getOperateId();
        BasicOperate byId3 = basicOperateService.getById(operateId);

        basicCarrierLineParam.setCreateId(user.getUserId());
        basicCarrierLineParam.setCreateUser(user.getName());
        basicCarrierLineParam.setCreateTime(new Date());
        basicCarrierLineParam.setStatus("0");
        basicCarrierLineParam.setCarrierName(byId1.getCarrierName());
        basicCarrierLineParam.setLineName(byId2.getLineName());
        basicCarrierLineParam.setOperateId(operateId);
        basicCarrierLineParam.setOperateName(byId3.getOperateName());
        basicCarrierLineParam.setDeptId(user.getDeptId());
        basicCarrierLineParam.setDeliverId(user.getDeliverId());

        this.basicCarrierLineService.add(basicCarrierLineParam);
        return ResponseData.success();
    }

    /**
     * 编辑接口
     *
     * @author like
     * @Date 2024-10-10
     */
    @RequestMapping("/editItem")
    @ResponseBody
    public ResponseData editItem(BasicCarrierLineParam basicCarrierLineParam) {
        User user = userService.getcurrentUser();
        Integer carrierId = basicCarrierLineParam.getCarrierId();
        BasicCarrier carrier = basicCarrierService.getById(carrierId);
        Integer lineId = basicCarrierLineParam.getLineId();
        BasicLine line = basicLineService.getById(lineId);

        basicCarrierLineParam.setUpdateId(user.getUserId());
        basicCarrierLineParam.setUpdateUser(user.getName());
        basicCarrierLineParam.setUpdateTime(new Date());
        basicCarrierLineParam.setCarrierName(carrier.getCarrierName());
        basicCarrierLineParam.setLineName(line.getLineName());
        this.basicCarrierLineService.update(basicCarrierLineParam);
        return ResponseData.success();
    }

    /**
     * 启用禁用开关
     * @param basicCarrierLineParam
     * @return
     */
    @RequestMapping("/disable")
    @ResponseBody
    public ResponseData disable(BasicCarrierLineParam basicCarrierLineParam) {
        basicCarrierLineParam.setUpdateId(userService.getcurrentUser().getUserId());
        basicCarrierLineParam.setUpdateUser(userService.getcurrentUser().getName());
        this.basicCarrierLineService.update(basicCarrierLineParam);
        return ResponseData.success();
    }

    /**
     * 删除接口
     *
     * @author like
     * @Date 2024-10-10
     */
    @RequestMapping("/delete")
    @ResponseBody
    public ResponseData delete(BasicCarrierLineParam basicCarrierLineParam) {
        this.basicCarrierLineService.delete(basicCarrierLineParam);
        return ResponseData.success();
    }

    /**
     * 查看详情接口
     *
     * @author like
     * @Date 2024-10-10
     */
    @RequestMapping("/detail")
    @ResponseBody
    public ResponseData detail(BasicCarrierLineParam basicCarrierLineParam) {
        BasicCarrierLine detail = this.basicCarrierLineService.getById(basicCarrierLineParam.getId());
        return ResponseData.success(detail);
    }

    /**
     * 查询列表
     *
     * @author like
     * @Date 2024-10-10
     */
    @ResponseBody
    @RequestMapping("/list")
    public LayuiPageInfo list(BasicCarrierLineParam basicCarrierLineParam,String condition ) {
        User user = userService.getcurrentUser();
        basicCarrierLineParam.setDeptId(user.getDeptId());
        basicCarrierLineParam.setDeliverId(user.getDeliverId());

        return this.basicCarrierLineService.findPageBySpec(basicCarrierLineParam,condition,user.getOperateId());
    }

}


